<?php

namespace App\Console\Commands;

use App\Events\SendBaoxiaoEvent;
use App\model\Project;
use App\Repository\WechatRepository;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

class SendTuiguangCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:tuiguang';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '给推广员发消息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       $project= Project::all();
       foreach ($project as $value){
           $user=User::whereRaw("FIND_IN_SET('$value->project_id',project_id)")->get();

           foreach ($user as $v){

               if ($v->hasRole("推广员")){
                   event(new SendBaoxiaoEvent($v,Carbon::now(),$v,$value));
                   $this->info($v->id);
                   $wechat=new WechatRepository();
                   $result=$wechat->sendTuiguangMessage($v,$value);
                   $this->info(json_encode($result));
               }
           }
       }
    }
}
